package com.zhudi.controller;

import com.zhudi.annotation.UserLoginCheck;
import com.zhudi.commen.BaseResponse;
import com.zhudi.commen.ErrorCode;
import com.zhudi.expection.BusinessException;
import com.zhudi.model.entity.User;
import com.zhudi.model.request.UserLoginRequest;
import com.zhudi.model.request.UserRegisterRequest;
import com.zhudi.model.vo.UserVO;
import com.zhudi.service.UserService;
import com.zhudi.utils.ResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.zhudi.contant.UserContant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    /**
     * 登录接口
     * @param userLoginRequest 封装的登录数据传输类
     * @param request 浏览器request
     * @return 登录成功的话返回用户的相关信息
     */
    @PostMapping("/login")
    public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (StringUtils.isAnyBlank(userLoginRequest.getUser_name(), userLoginRequest.getPassword())){
            throw new BusinessException(ErrorCode.USER_ERROR,"账户或密码为空");
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        if(loginUser!=null){
            String userName = loginUser.getUserName();
            //如果request中的用户名和要登陆的一致，说明已经登录了，那么就不用再次登录了
            if (userLoginRequest.getUser_name().equals(userName)){
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(loginUser,userVO);
                return ResultUtils.success(userVO,"不能重复登录");
            }
        }
        UserVO userVO = userService.login(userLoginRequest, request);
        if (userVO==null){
            return ResultUtils.Error(ErrorCode.PARAMS_ERROR,"用户名或密码错误");
        }
        return ResultUtils.success(userVO,"登录成功");
    }

    /**
     * 注册功能
     * @param request 浏览器request
     * @return 注册成功返回用户id，失败返回空
     */
    @PostMapping("/register")
    public BaseResponse<Integer> userRegister(@RequestBody UserRegisterRequest userRegisterRequest, HttpServletRequest request){

        if (StringUtils.isAnyBlank(userRegisterRequest.getUserName(), userRegisterRequest.getPassword(), userRegisterRequest.getConfirmPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"注册信息参数不足");
        }
        Integer result = userService.register(userRegisterRequest, request);

        return ResultUtils.success(result ,"注册成功");
    }

    /**
     * 注销功能
     * @param request 浏览器request
     * @return 成功返回0
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        return ResultUtils.success(userService.logout( request ),"注销成功");
    }

    @GetMapping("/get/login")
    @UserLoginCheck
    public BaseResponse<UserVO> getLoginUser(HttpServletRequest httpServletRequest){
        User user = (User)httpServletRequest.getSession().getAttribute(USER_LOGIN_STATE);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return ResultUtils.success(userVO,"获取脱敏后的用户信息成功");

    }

    @GetMapping("/getKeys")
    @UserLoginCheck
    public BaseResponse<UserVO> getKeys(HttpServletRequest httpServletRequest){
        User loginUser = (User) httpServletRequest.getSession().getAttribute(USER_LOGIN_STATE);

        UserVO result = userService.getKeys(loginUser);
        return ResultUtils.success(result,"查询成功");
    }

    @GetMapping("/getNewKeys")
    @UserLoginCheck
    public BaseResponse<UserVO> getNewKeys(HttpServletRequest httpServletRequest){
        User loginUser = (User) httpServletRequest.getSession().getAttribute(USER_LOGIN_STATE);

        UserVO result = userService.getNewKeys(loginUser);
        return ResultUtils.success(result,"生成成功");
    }
}
